package com.happy3w.ssp.schoolmatehome.controller;

import com.happy3w.ssp.schoolmatehome.entity.User;
import com.happy3w.ssp.schoolmatehome.interceptor.AttributeKeys;
import com.happy3w.ssp.schoolmatehome.service.auth.RegUserDto;
import com.happy3w.ssp.schoolmatehome.service.auth.TheUserDetail;
import com.happy3w.ssp.schoolmatehome.service.note.NoteSearcher;
import com.happy3w.ssp.schoolmatehome.service.note.NoteSummary;
import com.happy3w.ssp.schoolmatehome.service.user.UserInfo;
import com.happy3w.ssp.schoolmatehome.service.user.UserService;
import com.happy3w.ssp.schoolmatehome.service.user.UserSummary;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final UserService userService;
    private final NoteSearcher noteSearcher;

    @GetMapping("/{id}")
    public ResponseEntity<UserSummary> getUserById(@PathVariable Long id,
                                                   @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        return userService.getUserStateById(id, userDetail.getUser())
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/{id}/notes")
    public List<NoteSummary> findAllNotes(
            @PathVariable Long id,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return noteSearcher.findAllNotes(id, query, page, size);
    }

    @PostMapping("register")
    public UserInfo createUser(@RequestBody RegUserDto dto) {
        return userService.createUser(dto);
    }

    @PutMapping("/{id}/follow")
    @PreAuthorize("hasRole('ROLE_NAMED')")
    public void markFavorite(@PathVariable Long id, @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        userService.markFollow(id, userDetail.getUser());
    }

    @PutMapping("/{id}/unfollow")
    @PreAuthorize("hasRole('ROLE_NAMED')")
    public void unmarkFavorite(@PathVariable Long id, @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        userService.unmarkFollow(id, userDetail.getUser());
    }
}
